<template>
        <el-card shadow="never" style="max-height: 70vh; overflow: auto">
            <el-form ref="form" :model="form" :rules="rules" label-width="90px" :disabled="disabled">
                <el-form-item label="id:" prop="id" v-show="false">
                    <el-input v-model="form.id" disabled />
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="所属用户:" prop="user_id">
                            <SelectPage v-model="form.user_id" :url="'/admin/user/selectPage'" show_field="nickname" show_id="uid" query_field="uid" :multiple="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="姓名:" prop="name">
                            <el-input v-model="form.name" placeholder="请输入姓名" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="性别:" prop="sex">
                            <el-select v-model="form.sex" placeholder="请选择性别" clearable>
                                <el-option v-for="(item, index) in sex" :key="index" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="身份证号码:" prop="id_card">
                            <el-input v-model="form.id_card" placeholder="请输入身份证号码" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="生日:" prop="birthday">
                            <el-date-picker v-model="form.birthday" type="date" value-format="yyyy-MM-dd" placeholder="选择生日" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所属分类:" prop="category2" disabled>
                            <SelectCategory url="/admin/train.category/tree" ref="category" disabled
                                            :check-strictly="true"
                                            :value="[form.category1, form.category2, form.category3]"
                                            @change="onChangeCategory"></SelectCategory>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="收费:" prop="money">
                            <el-input-number v-model="form.money" :step="1" :min="0"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="简介描述:" prop="content">
                            <el-input v-model="form.content" placeholder="请输入简介描述" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="形象照片:" prop="images">
                    <ImageUpload v-model="form.images" :limit="10" />
                </el-form-item>
                <el-form-item label="资质证明:" prop="certificates">
                    <ImageUpload v-model="form.certificates" :limit="10" />
                </el-form-item>
                <el-form-item label="状态:" prop="status">
                    <el-select v-model="form.status" placeholder="请选择状态" clearable>
                        <el-option v-for="(item, index) in status" :key="index" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="未通过原因:" prop="fail_content" v-if="form.status === 3">
                    <el-input v-model="form.fail_content" placeholder="请输入未通过原因" clearable />
                </el-form-item>
                <el-form-item label="排序方式:" prop="sortweight">
                    <el-input-number v-model="form.sortweight" :step="1" :min="0"></el-input-number>
                </el-form-item>
                <el-form-item label="省名称:" prop="district" disabled>
                    <SelectCityArea ref="city" :checkStrictly="true" disabled
                                    :value="[form.province_id, form.city_id, form.district_id]"
                                    @change="onChangeCityArea"></SelectCityArea>
                </el-form-item>
                <el-form-item label="地址:" prop="address">
                    <el-input v-model="form.address" placeholder="请输入地址" clearable disabled />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm">确认 </el-button>
                    <el-button type="warning" @click="cancel">取消 </el-button>
                </el-form-item>
            </el-form>
        </el-card>
</template>

<script>
import SelectCategory from "@/components/My/SelectCategory";
import SelectCityArea from "@/components/My/SelectCityArea";
import { regionData } from 'element-china-area-data'
export default {
    name: 'EditForm',
    components: { SelectCategory, SelectCityArea },
    props: {
        findFormData: {
            type: Object,
            default: {}
        },
        editFormParams: {
            type: Object,
            default: {}
        },
        // 表单是否禁用
        disabled: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            // 编辑表单验证规则
            rules: {},
            form: {  },
            // 区域选择配置
            areaOptions: regionData,
            sex:[
               { label: "男", value: 1 },
               { label: "女", value: 2 },
               { label: "未知", value: 3 },
            ],
            status:[
               { label: "审核中", value: 1 },
               { label: "审核通过", value: 2 },
               { label: "审核失败", value: 3 },
            ],
        }
    },
    watch: {
        findFormData: {
           handler(nval) {
                if (JSON.stringify(nval) != '{}') {
                    this.$nextTick(() => {
                        this.form = nval
                    })
                }
            },
            immediate: true,
            deep: false,
        },
    },
    methods: {
        // 取消按钮
        cancel() {
            this.$emit('editFormCancel')
        },
        submitForm() {
            this.$refs['form'].validate((valid, obj) => {
                if (valid) {
                    this.$emit('submitForm', this.form)
                }
            })
        },
        // 表单重置
        reset() {
            this.resetForm('form')
        },
        onChangeCategory(e) {
            this.$set(this.form, 'category3', undefined)
            this.$set(this.form, 'category2', undefined)
            this.$set(this.form, 'category1', undefined)
            if (e.length >= 3) this.$set(this.form, 'category3', e[2])
            if (e.length >= 2) this.$set(this.form, 'category2', e[1])
            if (e.length >= 1) this.$set(this.form, 'category1', e[0])
        },
        onChangeCityArea(e) {
            this.$set(this.form, 'district_id', undefined)
            this.$set(this.form, 'province', undefined)
            this.$set(this.form, 'city_id', undefined)
            this.$set(this.form, 'city', undefined)
            this.$set(this.form, 'district', undefined)
            this.$set(this.form, 'province_id', undefined)
            if (e.length >= 3) this.$set(this.form, 'district_id', e[2])
            if (e.length >= 2) this.$set(this.form, 'city_id', e[1])
            if (e.length >= 1) this.$set(this.form, 'province_id', e[0])
        }
    }
}
</script>
